import {type FormSchema} from '#/../../../packages/@core/ui-kit/form-ui/src/types'
import {type VxeGridPropTypes} from '#/adapter/vxe-table'
import {getDictLabel, getDictOptions, getOptionsFromDict} from "#/utils";
import {AppTypes,AppGrade} from "#/views/toc/data";
import {ref} from "vue";
import {requestClient} from "#/api/request";

export const Category=ref<any[]>([])
export const form_schema:FormSchema<any>[]=[
  {
    component: 'ImageUpload',
    fieldName:'appIcon',
    label:'应用图标'
  },
  {
    // 组件需要在 #/adapter.ts内注册，并加上类型
    component: 'Input',
    // 对应组件的参数
    componentProps: {
      placeholder: '请输入',
    },
    // 字段名
    fieldName: 'appName',
    // 界面显示的label
    label: '应用名称',
    rules: 'required',
  },
  {
    component:'Select',
    componentProps:{
      options:getOptionsFromDict(AppTypes),
    },
    fieldName: 'appType',
    label:'应用类型',
    rules: 'required',
  },
  {
    component:'Input',
    componentProps:{
      type:'textarea'
    },
    dependencies:{
      show(values){
        return values.appType==='web'
      },
      required(values){
        return values.appType==='web'
      },
      triggerFields:['appType']
    },
    fieldName:'url',
    label:"链接",
  },
  {
    component:'Input',
    fieldName:'router',
    label:'路由',
    dependencies:{
      show(values){
        return values.appType==='platform'
      },
      required(values){
        return values.appType==='platform'
      },
      triggerFields:['appType']
    }
  },
  {
    component:'Select',
    fieldName:'appGrade',
    label:'适用年级',
    componentProps:{
      options:getOptionsFromDict(AppGrade),
      multiple:true
    }
  },
  {
    component:'TreeSelect',
    fieldName:'ccategoryId',
    label:'应用类型',
    componentProps:{
      data:[],
      lazy:true,
      isLeaf:'leaf',
      //@ts-ignore
      load(node:any,resolve){
        console.log("node",node)
        requestClient.get('/platform/app-category/list_by_pid',{params:{pid:node.data?.value||0}}).then((d:any[])=>{
          resolve(
            d.map((e:any)=>{
              return {
                value:e.id,
                label:e.appCategoryName,
                leaf:true
              }
            })
          )
        })
      }
    }
  },
  {
    component:'Select',
    fieldName:'subjectName',
    label:'绑定学科',
    componentProps:{
      options: getDictOptions('subject_name','string')
    },
  },
  {
    component:'Switch',
    fieldName:'useCarefully',
    label:'精选应用'
  }
]

export const table_columns:VxeGridPropTypes.Columns=[
  { field: 'appIcon', title: '应用图标',fixed:'left',minWidth:150,
    slots:{default:'appIcon'}
  },
  { field: 'appName', title: '应用名称',fixed:'left',minWidth:150 },
  { field: 'appType', title: '应用类型',
    formatter:({row})=>{return AppTypes[row.appType] as string},
    minWidth:150
  },
  { field: 'appGrade', title: '适用年级',
    formatter:({row})=>row.appGrade?.map((g:any)=>AppGrade[g]).join(','),
    minWidth:150,
  },
  { field:'ccategoryName',title:'应用类型',minWidth:150},
  { field:'subjectName',title:'应用学科',
    formatter:({row})=>row.subjectName?getDictLabel('subject_name',row.subjectName):'',
    minWidth:150
  },
  { title: '操作',slots:{default:'option'},fixed:'right',minWidth:150}
]
